package com.teamide.client;

import java.io.Serializable;

import com.alibaba.fastjson.JSONObject;
import com.teamide.bean.UserBean;

/**
 * 客户端会话 通常会话将放在HttpSession中，控制用户登录、登出、存放会话的缓存
 * 
 * @author ZhuLiang
 *
 */
public class ClientSession implements Serializable, Cloneable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private boolean login;

	private UserBean user;

	private final JSONObject cache = new JSONObject();

	@Override
	public ClientSession clone() throws CloneNotSupportedException {
		ClientSession session = (ClientSession) super.clone();
		session.login = this.login;
		session.user = this.user;
		session.cache.putAll(this.cache);
		return session;
	}

	public void destroy() {
		cache.clear();
		user = null;
		login = false;
	}

	public boolean isLogin() {

		return login;
	}

	public void doLogin(UserBean user) {

		this.user = user;
		this.login = user != null;
	}

	public void doLogout() {

		this.user = null;
		this.login = false;
	}

	public UserBean getUser() {

		return user;
	}

	public JSONObject getCache() {

		return cache;
	}

	public void setCache(String key, Object value) {

		this.cache.put(key, value);
	}

	public void set(String key, Object value) {

		this.cache.put(key, value);
	}

	public Object get(String key) {
		return this.getCache(key);
	}

	public <T> T get(String key, Class<T> clazz) {
		return this.getCache(key, clazz);
	}

	public Object getCache(String key) {

		return this.cache.get(key);
	}

	public <T> T getCache(String key, Class<T> clazz) {
		return this.cache.getObject(key, clazz);
	}

	public Object removeCache(String key) {

		return this.cache.remove(key);
	}

	public void clear() {

		this.cache.clear();
	}

}
